/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fuzzy.actions;

import fuzzy.hibernate.HibernateUtil;
import fuzzy.hibernate.Klienci;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author kaqu
 */
public class FindAction extends DispatchAction {

    public ActionForward enter(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("find");
    }

    private int getFuzzyLevelValue(String str) {
        if (str.equals("High")) {
            return 2;
        } else if (str.equals("Medium")) {
            return 1;
        } else {
            return 0;
        }
    }

    public ActionForward findPerson(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("FIND AJAX");
        FindForm ajaxForm = (FindForm) form;
        int fuzzy1, fuzzy2;
        fuzzy1 = getFuzzyLevelValue(ajaxForm.getCount());
        fuzzy2 = getFuzzyLevelValue(ajaxForm.getPossibility());

        System.out.println("aaaa " + fuzzy1);
        System.out.println("aaaa " + fuzzy2);
        response.setContentType("text/text;charset=utf-8");

        response.setHeader("cache-control", "no-cache");

        Session session = HibernateUtil.getSession();
        Query query = session.createSQLQuery(
                "SELECT ZNAJDZKREDYTOBIORCE( :arg1 , :arg2) FROM dual")//GETFUZZYPERSONLIST
                .setParameter("arg1", fuzzy1).setParameter("arg2", fuzzy2);
        List result = query.list();
        System.out.println("result " + result);
        //System.out.println("result " + result);

        PrintWriter out = response.getWriter();
        
        if(result.get(0)==null)
        {
            out.println("There is no person to suggest a credit.");
        }
        else
        {
            out.println("You can suggest a credit for:<br>");
            //out.println(result.get(0));
            Session currentSession = HibernateUtil.getSession();
            currentSession.beginTransaction();
            
            // Pobieramy wszystkich klientów z bazy.
            Query q = currentSession.createQuery("FROM Klienci where "+result.get(0));
            List<Klienci> klienci = q.list();
            List<ClientActionForm> clients = new ArrayList();
            out.println("<table>\n" +
"    <tr>\n" +
"        <th>#</th>\n" +
"        <th>Firstname</th>\n" +
"        <th>Lastname</th>\n" +
"        <th>PESEL</th>\n" +
"        <th>Address</th>\n" +
"    </tr>");
            // Tworzymy obiekty beana ClientActionForm.
            for (Klienci klient: klienci) {
                out.println("<tr>");
                ClientActionForm client = new ClientActionForm();
                
                client.setClientId(klient.getIdklienta());
                out.println("<th>"+klient.getIdklienta()+"</th>");
                client.setFirstname(klient.getImie());
                out.println("<th>"+klient.getImie()+"</th>");
                client.setLastname(klient.getNazwisko());
                out.println("<th>"+klient.getNazwisko()+"</th>");
                client.setPesel(klient.getPesel());
                out.println("<th>"+klient.getPesel()+"</th>");
                client.setAddress(klient.getAdres());
                out.println("<th>"+klient.getAdres()+"</th>");
                clients.add(client);
                out.println("</tr>");
            }
            out.println("</table>");
        }
        
        for (Object o : result) {
            System.out.println("object " + o);
        }
        //out.println(result.get(0));
        out.flush();

        return null;
    }

}
